"use strict";
const Service = require("egg").Service;
const { httpCode } = require("../../utils/userConfig")
const crypto = require('crypto');
const AuthException = require('../../exception/authException');
const SystemErrException = require("../../exception/systemError");
const Qiniu = require("../../utils/qiniu");
const { Op } = require("sequelize");

class MonitorPartnersService extends Service {

  async list(requestData) {
    try {
      const { ctx, app } = this;
      const {
        pageSize = 100,
        currentPage = 1,
        unitip
      } = requestData;

      let searchCondition = {}
      if (unitip) {
        searchCondition.unitip = unitip
      }
      let { count, rows } = await ctx.model.Videos.Logs.findAndCountAll({
        where: searchCondition,
        raw: true,
        offset: (currentPage - 1) * pageSize,
        limit: Number(pageSize),
        order: [['datetime', 'desc']]
      });

      return {
        data: {
          data: rows,
          count,
        },
        code: httpCode.SUCCESS,
        message: '设备组查询成功！'
      }
    } catch (error) {
      this.ctx.logger.error(requestData);
      throw new SystemErrException("设备组查询异常：" + error);
    }
  }

  async unusualClear({ partnerId }) {
    try {
      const { ctx, app } = this;
      await ctx.model.Videos.UnusualLogs.destroy({
        where: {
          partnerId
        },
      });
      return {
        code: httpCode.SUCCESS,
        message: '设备组异常日志删除成功！'
      }
    } catch (error) {
      this.ctx.logger.error(requestData);
      throw new SystemErrException("设备组异常日志删除异常：" + error);
    }
  }

  async logsClear({ unitip }) {
    try {
      const { ctx, app } = this;
      await ctx.model.Videos.Logs.destroy({
        where: {
          unitip
        },
      });
      return {
        code: httpCode.SUCCESS,
        message: '设备日志删除成功！'
      }
    } catch (error) {
      this.ctx.logger.error(requestData);
      throw new SystemErrException("设备日志删除异常：" + error);
    }
  }

  async unusual(requestData) {
    try {
      const { ctx, app } = this;
      const {
        pageSize = 100,
        currentPage = 1,
        partnerId
      } = requestData;

      let searchCondition = {}
      if (partnerId) {
        searchCondition.partnerId = partnerId;
      }
      let { count, rows: unusualLogs } = await ctx.model.Videos.UnusualLogs.findAndCountAll({
        where: searchCondition,
        raw: true,
        offset: (currentPage - 1) * pageSize,
        limit: Number(pageSize),
        order: [['createDate', 'desc']]
      });

      const monitorPartners = (await ctx.service.videos.monitorPartners.list({ ip: '' })).data.data;

      let statusOpts = ['设防中', '滞留车辆数预警', '滞留时间预警', '警报中', '排查中', '停止工作'];
      unusualLogs.forEach(log => {
        let cur = monitorPartners.filter(item => item.id == log.partnerId)[0];
        log.partnerName = cur.partnerName;
        log.statusName = statusOpts[log.status - 1];
      })
      return {
        data: {
          data: unusualLogs,
          count,
        },
        code: httpCode.SUCCESS,
        message: '设备组异常日志查询成功！'
      }
    } catch (error) {
      this.ctx.logger.error(requestData);
      throw new SystemErrException("设备组异常日志查询异常：" + error);
    }
  }

  async types() {
    try {
      const { ctx, app } = this;
      let machines = await ctx.model.Videos.Logs.count({
        group: ['unitip'],
      });
      machines.forEach(machine => {
        machine.name = machine.unitip;
        delete machine.count
        delete machine.unitip
      })
      return {
        data: machines,
        code: httpCode.SUCCESS,
        message: 'IP列表查询成功！'
      }
    } catch (error) {
      this.ctx.logger.error(requestData);
      throw new SystemErrException("IP列表查询异常：" + error);
    }
  }

  async mocking(log) {
    try {
      const { ctx, app } = this;
      await ctx.model.Videos.Logs.create(log);
      return {
        code: httpCode.SUCCESS,
        message: '数据模拟成功！'
      }
    } catch (error) {
      this.ctx.logger.error(requestData);
      throw new SystemErrException("数据模拟异常：" + error);
    }
  }
}

module.exports = MonitorPartnersService;
